package com.junyuan.gsp.util;

import com.junyuan.gsp.bean.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * @Author: maxwellens
 */
public class SubjectUtil
{
    public static final String STATION_ID = "stationId";

    public static User getUser()
    {
        Subject subject = SecurityUtils.getSubject();
        Object user = subject.getPrincipal();
        if (user == null)
        {
            return null;
        }
        if (user instanceof User)
        {
            return (User)user;
        } else
        {
            return null;
        }
    }

    public static void setAttribute(String key, Object value)
    {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        if (session == null)
        {
            return;
        }
        session.setAttribute(key, value);
    }

    public static Object getAttribute(String key)
    {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        if (session == null)
        {
            return null;
        }
        return session.getAttribute(key);
    }

    public static void setStationId(Integer id)
    {
        setAttribute(STATION_ID, id);
    }

    public static Integer getStationId()
    {
        return (Integer) getAttribute(STATION_ID);
    }
}
